package common

import (
	"github.com/spf13/viper"
)

// EmailConfig contains the email configuration loaded from application.yml
type EmailConfig struct {
	Host         string
	PortTLS      int
	PortSTARTTLS int
	Username     string
	Password     string
	From         string
	Recipient    string
}

var emailConfig *EmailConfig

// LoadEmailConfig loads email configuration from application.yml
func LoadEmailConfig() *EmailConfig {
	if emailConfig == nil {
		emailConfig = &EmailConfig{
			Host:         viper.GetString("email.smtp.host"),
			PortTLS:      viper.GetInt("email.smtp.port_tls"),
			PortSTARTTLS: viper.GetInt("email.smtp.port_starttls"),
			Username:     viper.GetString("email.smtp.username"),
			Password:     viper.GetString("email.smtp.password"),
			From:         viper.GetString("email.smtp.from"),
			Recipient:    viper.GetString("email.recipient"),
		}
	}
	return emailConfig
}

// GetEmailConfig returns the cached email configuration
func GetEmailConfig() *EmailConfig {
	if emailConfig == nil {
		return LoadEmailConfig()
	}
	return emailConfig
}
